﻿Imports System.IO

Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim sourceText = RichTextBox1.Text
        Dim distPath = TextBox1.Text
        'instance 是 fileUnits类的一个实例
        Dim instance = New fileUnits
        instance.writeToFile(sourceText, distPath)
    End Sub

    Private Sub Form1_load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        RichTextBox1.Text = ""
    End Sub

    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        RichTextBox2.Text = ""
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Dim instance = New fileUnits
        Dim sourcePath = TextBox2.Text
        If sourcePath.Length <> 0 Then
            Dim sourceContent = instance.readFromFile(sourcePath)
            RichTextBox2.Text = sourceContent
        Else
            MsgBox("路径不能为空")
        End If
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs)
        MsgBox(DateTime.Now.Ticks)
    End Sub

    Private Sub Button5_Click_1(sender As Object, e As EventArgs) Handles Button5.Click
        SaveFileDialog1.Filter = "(*.txt)|*.txt"
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            TextBox1.Text = SaveFileDialog1.FileName
        End If
    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click
        If OpenFileDialog1.ShowDialog() = DialogResult.OK Then
            TextBox2.Text = OpenFileDialog1.FileName
        End If
    End Sub
End Class

'fileUnits是一个用户自定义的工具类
Public Class fileUnits
    '定义一个写文件操作的sub函数
    Public Sub writeToFile(content As String, path As String)
        Dim swriter = New StreamWriter(path)
        swriter.WriteLine(content)
        swriter.Close()
    End Sub
    Public Function readFromFile(path As String) As String
        Dim sreader = New StreamReader(path)
        Dim content = sreader.ReadLine()
        Return content
    End Function
End Class